package com.fengba.nanzhi.tabbar04.wz;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.renderscript.Allocation;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.j256.ormlite.stmt.query.In;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

/**
 * Created by 89311 on 2018/4/11.
 */

public class Card1Adapter extends RecyclerView.Adapter<Card1Adapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private JSONObject job;
    private String bank_name;


    public Card1Adapter(Context context, JSONArray jsonArray1) {
        this.context = context;
        this.jsonArray = jsonArray1;
    }

    @Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.wz_card1_item_view,parent,false));
    }


    @Override
    public void onBindViewHolder(LinerViewHolder holder, int position) {

//

        String Cardname="";
        String Cardid="";
        try {
            job = (JSONObject) jsonArray.get(position);
            bank_name = job.get("bankcode").toString();
            Cardname = job.get("bankname").toString();
            Cardid = job.get("cardnumber").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        holder.mCardName.setText(Cardname);
        holder.mCardiId.setText(Cardid);

        String new_bank_name = bank_name.toLowerCase();
        Bitmap bitmap = getBitmapByName("bank_"+new_bank_name);
        holder.cardimg.setImageBitmap(bitmap);
    }

    @Override
    public int getItemCount() {
        if(jsonArray!=null) {
            return jsonArray.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView mCardName;
        private TextView mCardiId;
        private ImageView cardimg;
        public LinerViewHolder(View itemView) {
            super(itemView);
            mCardName = itemView.findViewById(R.id.cardname);
            mCardiId = itemView.findViewById(R.id.cardid);
            cardimg = itemView.findViewById(R.id.cardimg);
        }
    }
    public Bitmap getBitmapByName(String name) {
        ApplicationInfo appInfo = context.getApplicationInfo();
        int resID = context.getResources().getIdentifier(name, "mipmap", appInfo.packageName);
        return BitmapFactory.decodeResource(context.getResources(), resID);
    }
}
